import {
    getSecuBlocks,
    getSecuExpect,
    getSecuInfo,
    getSecuPrice,
    getTopRanks,
} from "@/backend/services/secu-service";
import styles from "./page.module.scss"
import {uniqueArray} from "@/lib/utils";


type BlockType = {
    secu_code: string,
    block_name: string,
    block_explain: string,
    block_source: string
}

type RankType = {
    rank_name: string,
}
//
// async function RiskArea() {
//     const cached = await memorizedETFDown(150)
//     // console.log(cached.length)
//     const rankHist = await getRankHist()
//     const etfData = structuredClone(cached)
//     const filtered = etfData.filter((etf: { f14: string; f3: number; label: string; }) => {
//         for (const plate of rankHist) {
//             if (etf.f14.toLowerCase().includes(plate.toLowerCase()) && (etf.f3 < 0)) {
//                 etf.label = plate
//                 return true
//             }
//         }
//         etf.label = ""
//         return false
//     })
//     const uniqueEtfs = filtered.filter((etf: any, index: any, selfArr: any) => index === selfArr.findIndex((t: any) => t.label === etf.label))
//     console.log(uniqueEtfs)
//     return <div>
//         {
//             uniqueEtfs.map((etf, index) => <p key={index}>{etf.f14}</p>)
//         }
//     </div>
// }

export default async function InfoPage({params}: { params: Promise<{ code: string }> }) {
    const {code} = await params;
    const info = await getSecuInfo(code);
    if (!info) return null;
    const price = await getSecuPrice(code);
    const expect = await getSecuExpect(code);
    const blocks: BlockType[] = await getSecuBlocks(code) as BlockType[];
    // 风格
    const blocksSTY = blocks.filter((b: { block_name: string; }) =>
        b.block_name?.includes('重组') ||
        b.block_name?.includes('摘帽') ||
        b.block_name?.includes('国企') ||
        b.block_name?.includes('央企') ||
        b.block_name?.includes('增长')
    );
    const topRanks: RankType[] = await getTopRanks() as RankType[];
    const topRankNames = topRanks.map((r: { rank_name: string; }) => r.rank_name);
    // 共振
    const filteredBlocks = []
    for (const rankName of topRankNames) {
        for (const block of blocks) {
            if (rankName.toLowerCase().includes(block.block_name?.toLowerCase())) {
                filteredBlocks.push(block);
                continue;
            }
            if (block.block_name?.toLowerCase().includes(rankName.toLowerCase())) {
                filteredBlocks.push(block);
            }
        }
    }
    const blocksTDX = uniqueArray(filteredBlocks.filter(b => b.block_source === 'tdx'), 'block_name')
    const blocksTHS = uniqueArray(filteredBlocks.filter(b => b.block_source === 'ths'), 'block_name')
    const blocksCLS = uniqueArray(filteredBlocks.filter(b => b.block_source === 'cls'), 'block_name')
    const blocksKPL = uniqueArray(filteredBlocks.filter(b => b.block_source === 'kpl'), 'block_name')
    // 去重
    const uniqueBlocks = filteredBlocks.filter((block, index, selfArr) =>
        index === selfArr.findIndex(b => b.block_name === block.block_name)
    )
    return <div className={styles.infoContainer}>
        <div id='secuInfo' className={styles.secuInfo}>
            <div id='left' className={styles.left}>
                <div className={styles.info}>
                    <div className={styles.infoItem}>{info.secu_code}</div>
                    <div className={styles.infoItem}>{info.secu_name}</div>
                    <div className={styles.infoItem}>{info.hy} {'=>'} {info.hy2}</div>
                    <div className={styles.infoItem}></div>
                </div>
                <div className={styles.info}>
                    <div className={styles.infoItem}>市场：{info.secu_code.startsWith('6') ? '沪' : '深'}</div>
                    <div className={styles.infoItem}>流通：{info.ltsz}</div>
                    <div className={styles.infoItem}>价格：{price}</div>
                    <div className={styles.infoItem}></div>
                </div>
                <hr/>
                <div className={styles.infoRow}>
                    <span>描述 ：{info.describe}</span>
                </div>
                <div className={styles.infoRow}>
                    <span>亮点 ：{info.profitable_business}</span>
                </div>
                <div className={styles.infoRow}>
                    <span>主营 ：{info.main_business}</span>
                </div>
                <div className={styles.infoRow}>
                    <span>产品 ：{info.product_name}</span>
                </div>
                <hr/>
                <div>
                    <span style={{color: 'white', display: 'block'}}>EPS ：{expect?.expect.join('；')}</span>
                    <span
                        style={{
                            color: 'white',
                            display: 'block'
                        }}>STY ：{blocksSTY.map(b => b.block_name).join('；')}</span>
                    <span style={{
                        color: 'cyan',
                        display: 'block'
                    }}>CLS ：{blocksCLS.map(b => b.block_name).join('；')}</span>
                    <span style={{
                        color: 'cyan',
                        display: 'block'
                    }}>TDX ：{blocksTDX.map(b => b.block_name).join('；')}</span>
                    <span style={{
                        color: 'red',
                        display: 'block'
                    }}>KPL ：{blocksKPL.map(b => b.block_name).join('；')}</span>
                    <span style={{
                        color: 'red',
                        display: 'block'
                    }}>THS ：{blocksTHS.map(b => b.block_name).join('；')}</span>
                </div>
            </div>
        </div>
        <hr/>
        <div id='blockDetails'>
            {
                uniqueBlocks.map((b, i) => (
                    <p className={styles.blockRow} key={i}>{b.block_name}：{b.block_explain}</p>
                ))
            }
        </div>
    </div>

}